<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: gavin
 * Date: 15-3-25
 * Time: 下午5:31
 */
class Ghc_user_model extends CI_Model
{

    public function __construct()
    {
        parent::__construct();
        //装载database
        $this->load->database();
    }

    /**
     * @param $username
     * @param $password
     * @param null $nickname
     * @param string $role
     * @param null $photo
     * @param null $email
     * @param null $description
     * 默认插入
     */
    public function insert($username,$password,$nickname=null,$role='admin',$photo=null,$email=null,$description=null,$lastlogindate)
    {
        $value['username'] = $username;
        $value['password'] = $password;
        $value['nickname'] = $nickname;
        $value['role'] = $role;
        $value['photo'] = $photo;
        $value['email'] = $email;
        $value['description'] = $description;
        $value['lastlogindate'] = $lastlogindate;
        $this->db->insert('ghc_user',$value);
    }

    /**
     * @return mixed
     * 获取第一个用户
     */
    public function getFirstUser()
    {
        $sql = sprintf("select * from %s ORDER BY uid",'ghc_user');
        $query = $this->db->query($sql);
        return $query;
    }

    /**
     * @param $username
     * @param $password
     * @return int
     * 判断用户是否存在 返回uid
     * 增加登录次数logins+1
     * 如果不存在
     *      判断用户是否存在
     *          不存在
     *          loginerror+1
     *          存在
     *          lastlogintime++
     */
    public function isUser($username,$password)
    {
        $sql = sprintf("select uid from ghc_user where username like '%s' and password like '%s'",$username,$password);
        $query = $this->db->query($sql);
        if($query->num_rows() == 0)
        {
            $sql = sprintf("select uid from ghc_user where username like '%s' ",$username);
            $query = $this->db->query($sql);
            if($query->num_rows() != 0)
            {
                $sql = sprintf("update ghc_user set loginerror = loginerror+1 where username like '%s' ",$username);
                $this->db->query($sql);
            }
            return 0;
        }
        else {
            $lastlogindate = time();
            $sql = sprintf("update ghc_user set logins = logins+1, lastlogindate=%s where username like '%s' ",$lastlogindate,$username);
            $this->db->query($sql);
            return $query->first_row()->uid;
        }
    }

    /**
     * @return mixed
     * 获取所有用户信息
     */
    public function getUsers()
    {
        $sql = sprintf("select * from ghc_user");
        $query = $this->db->query($sql);
        return $query->result();
    }

    /**
     * @param $uid
     * @return mixed
     * 获取用户的username 通过uid
     */
    public function getUsernameByUid($uid)
    {
        $sql = sprintf("select username from ghc_user where uid=%s",$uid);
        $query = $this->db->query($sql);
        return $query->first_row()->username;
    }

    /**
     * @param $uid
     * @return mixed
     * 获取单个用户所有信息
     */
    public function getUserByUid($uid)
    {
        $sql = sprintf("select * from ghc_user where uid=%s",$uid);
        $query = $this->db->query($sql);
        return $query->first_row();
    }
}